home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6101 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Do you have ever pass structures?
  5. Date: 22 Feb 96 13:21:00 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.824995260@rscernix>
  8. References: <4ge8mi$qjm@srvr1.engin.umich.edu> <4ggs0r$2bt@newshost.cyberramp.net>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4ggs0r$2bt@newshost.cyberramp.net> sinan@cyberramp.net (John Noland) writes:
  13.  
  14. >Anyway, I'll give you this much (but you should still read K&R), C passes 
  15. >all arguments except arrays by value - a copy of a variable is passed to
  16. >a function on the stack. (You could argue - correctly - that arrays are 
  17. >not really an exception because C passes the pointer to the first element
  18. >by value, but that isn't the way it's usually presented).
  19.  
  20. Arrays are an exception, indeed, because they're not passed as arguments
  21. to functions at all :-)  In a value context, an array name decays to a
  22. pointer to the first element and that pointer is passed using the single
  23. method of passing arguments supported by C: by value.
  24.  
  25. However, there is a workaround for passing arrays by value: wrap them in
  26. a structure!  Whether this is a good idea or not is up to every programmer
  27. to decide for his particular application.
  28.  
  29. Another point: some implementations don't really pass structures by value,
  30. they only pretend to!  They caller makes a local copy of the struct and
  31. passes a pointer to that copy.  The callee transparently dereferences
  32. that pointer to access the struct members.  When the callee returns, the
  33. caller discards the copy.  This has an interesting consequence for broken
  34. code, like this:
  35.  
  36.     /* void foo(const struct bar *p); this prototype is not in the scope 
  37.                                of the caller */
  38.     struct bar baz;
  39.     foo(baz);
  40.  
  41. With a prototype for foo in scope, the compiler would detect the error.
  42. If such a prototype doesn't exist (maybe because the compiler is old
  43. enough to not support prototypes but new enough to pass structures 
  44. "by value") the compiler will silently accept the code and the code will
  45. "work" as intended because the caller actually sends a pointer to a
  46. copy of baz and the callee expects a pointer to struct bar.
  47.  
  48. The code will "misteriously" crash and burn when ported to a different
  49. platform, which actually passes structures by value.
  50.  
  51. Dan
  52. --
  53. Dan Pop
  54. CERN, CN Division
  55. Email: danpop@mail.cern.ch 
  56. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  57.